package com.jml.controller;

import org.springframework.context.EnvironmentAware;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.FlashMap;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springframework.web.servlet.support.RequestContextUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;
import java.util.Map;

@Controller
public class GoController implements EnvironmentAware {

    /*这里单独写了处理HEAD请求的方法，此方法可以用来检测服务器的状态，因为它不返回body所以比GET请求更节省网络资源。
     *而单独写一个处理方法而不跟GET请求使用同个方法，然后返回没有Body的 Response
     * 是因为GET请求的处理过程可能会处理一些别的内容，
     * 如初始化一些首页需要显示的内容，还可能会连接数据库，而这些都比较浪费资源，
     * 并且对于HEAD请求来说也是不需要的，所以最好单独写一个方法。
    */
    @RequestMapping(value= {"/"},method={RequestMethod.HEAD})
    public String head(){
        return "go.jsp";
    }
    /*
    * 如果没有配置ViewResolver,
    * Spring Mvc将默认使用 org.springframework.web.servlet.view.InternalResource.ViewResolver
    * 作为ViewResolver，而且 prefix和sux都为空。所以go.sp返回值对应的就是根目录下的 go.sp文件。我们就把它建出来。
    * */
    @RequestMapping(value= {"/index","/"},method={RequestMethod.GET})
    public String index(Model model){
        model.addAttribute("msg","GO go go");
        return "go.jsp";
    }

    private Environment environment = null;

    @Override
    public void setEnvironment(Environment environment) {
        this.environment = environment;
    }


    @RequestMapping(value= "/submit" , method={RequestMethod.POST})
    public String submit (RedirectAttributes attr) {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request001 = requestAttributes.getRequest();
        FlashMap f = (FlashMap)request001.getAttribute(DispatcherServlet.OUTPUT_FLASH_MAP_ATTRIBUTE);
        f.put("name","张三丰");
        //这种方式的参数会放入到FlashMap中
        attr.addFlashAttribute("ordersId","XXX");
        //这个方式加入的参数只会拼接到url中不会放在FlashMap中
        attr.addAttribute("local","zh-cn");
        return "redirect:showorders";
    }

    @RequestMapping(value= "/showorders" , method=RequestMethod.GET)
    public String showorders (Model model,HttpServletRequest request) {
        //这种方式也可以得到FlatMap,原理都一样都是从RequestContextHolder中获取的
        Map<String, ?> inputFlashMap = RequestContextUtils.getInputFlashMap(request);
        Enumeration<String> attributeNames = request.getAttributeNames();
        System.out.println(attributeNames.toString());
        System.out.println(inputFlashMap.get("name"));
        System.out.println(inputFlashMap.get("ordersId"));
        System.out.println(request.getAttribute("local"));
        return "orders";
    }

    @RequestMapping(value= "/jml-handler/" , method=RequestMethod.GET)
    public String handler (Model model,HttpServletRequest request) {
        return "go.jsp";
    }
}
